//
//  Low_Wnd_View.m
//  ICE_Player
//
//  Created by lijinxin on 11-10-23.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "Low_Wnd_View.h"
#import "Globalize.h"
#import <QuartzCore/QuartzCore.h>

@implementation Low_Wnd_View

@synthesize p_bg_img;
@synthesize p_preview;
@synthesize p_del_btn;

@synthesize p_label_file_size;
@synthesize p_label_file_size_val;
@synthesize p_label_duration;
@synthesize p_label_duration_val;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc {
	[self destroy_SubViews];
    [super dealloc];
}

- (void)set_view_pos
{
    CGRect super_rect = {0};
    CGRect cur_rect = {0};
    super_rect = self.superview.frame;
    cur_rect = self.frame;
    self.frame = CGRectMake(super_rect.origin.x, super_rect.size.height - cur_rect.size.height, cur_rect.size.width, cur_rect.size.height);
    super_rect = self.frame;
    cur_rect = p_bg_img.frame;
    p_bg_img.frame = CGRectMake(super_rect.origin.x + cur_rect.origin.x, super_rect.origin.y + cur_rect.origin.y, cur_rect.size.width, cur_rect.size.height);
    super_rect = self.frame;
    cur_rect = p_preview.frame;
    p_preview.frame = CGRectMake(super_rect.origin.x + cur_rect.origin.x, super_rect.origin.y + cur_rect.origin.y, cur_rect.size.width, cur_rect.size.height);
    super_rect = self.frame;
    cur_rect = p_del_btn.frame;
    p_del_btn.frame = CGRectMake(super_rect.origin.x + cur_rect.origin.x, super_rect.origin.y + cur_rect.origin.y, cur_rect.size.width, cur_rect.size.height);
    super_rect = self.frame;
}

- (void)create_SubViews:(UIViewController*)controller
{
    [super create_SubViews:controller];
    
    CREATE_ICE_VIEW(p_bg_img, ICE_Image_View, controller);
    CREATE_ICE_VIEW(p_preview, Preview_Image_View, controller);
    CREATE_ICE_VIEW(p_del_btn, Delete_File_Btn, controller);
    
    CREATE_ICE_VIEW_GLOBALIZE(p_label_file_size, ICE_Label_View, controller, EG_LBL_ID_LOW_WND_SIZE);
    CREATE_ICE_VIEW(p_label_file_size_val, ICE_Label_View, controller);
    CREATE_ICE_VIEW_GLOBALIZE(p_label_duration, ICE_Label_View, controller, EG_LBL_ID_LOW_WND_DURATION);
    CREATE_ICE_VIEW(p_label_duration_val, ICE_Label_View, controller);
    
    [p_bg_img addSubview:p_label_file_size];
    [p_bg_img addSubview:p_label_file_size_val];
    [p_bg_img addSubview:p_label_duration];
    [p_bg_img addSubview:p_label_duration_val];
    
    [self set_view_pos];
    
    p_bg_img.hidden = TRUE;
    p_preview.hidden = TRUE;
    p_del_btn.hidden = TRUE;
    p_label_file_size.hidden = TRUE;
    p_label_file_size_val.hidden = TRUE;
    p_label_duration.hidden = TRUE;
    p_label_duration_val.hidden = TRUE;
    self.hidden = TRUE;
    
    p_label_file_size.text = IGS(EG_STR_ID_LOW_WND_SIZE);
    p_label_duration.text = IGS(EG_STR_ID_LOW_WND_DURATION);
}

- (void)scale_to_fit_iPad
{
    UIView* top_view = self;
    UIResponder* pctl = nil;
    CGRect rect;
    CGPoint pnt;
    CGRect p_rect;
    
    while (top_view != nil)
    {
        //go to top view
        pctl = [top_view nextResponder];
        if (pctl && [pctl isKindOfClass:[UIView class]])
        {
            top_view = (UIView*)pctl;
            continue;
        }
        //get controller
        if (pctl && [pctl isKindOfClass:[UIViewController class]])
        {
            {
                p_rect = ((UIViewController*)pctl).view.bounds;
            }
            break;
        }
        break;
    }
    
    rect = self.bounds;
    rect.size.width = p_rect.size.width;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    self.bounds = rect;
    self.center = pnt;
    
    rect = p_del_btn.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width;
    rect.origin.y += 10.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_del_btn.bounds = rect;
    p_del_btn.center = pnt;
}

- (void) setHidden:(BOOL)enabled
{
    p_del_btn.hidden = enabled;
    p_preview.hidden = enabled;
    p_bg_img.hidden = enabled;
    p_label_file_size.hidden = enabled;
    p_label_file_size_val.hidden = enabled;
    p_label_duration.hidden = enabled;
    p_label_duration_val.hidden = enabled;
	super.hidden = enabled;
}

- (void)destroy_SubViews
{
    RELEASE_ICE_VIEW(p_label_duration_val);
    RELEASE_ICE_VIEW(p_label_duration);
    RELEASE_ICE_VIEW(p_label_file_size_val);
    RELEASE_ICE_VIEW(p_label_file_size);
    
    RELEASE_ICE_VIEW(p_del_btn);
    RELEASE_ICE_VIEW(p_preview);
    RELEASE_ICE_VIEW(p_del_btn);
}

- (void)On_Show:(char*)url parent_view:(UIView*)parent
{
    if (self.hidden)
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionPush;//@"rippleEffect";//110
        animation.subtype = kCATransitionFromTop;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
        animation.delegate = p_bg_img;
        [p_bg_img.layer addAnimation:animation forKey:@"animation"];
        
        self.hidden = FALSE;
        p_bg_img.hidden = FALSE;
        p_preview.hidden = FALSE;
        p_del_btn.hidden = FALSE;
        p_label_file_size.hidden = FALSE;
        p_label_file_size_val.hidden = FALSE;
        p_label_duration.hidden = FALSE;
        p_label_duration_val.hidden = FALSE;
    }
}

- (void)On_Leave
{
    if (!self.hidden)
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = p_bg_img;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionReveal;//@"rippleEffect";//110
        animation.subtype = kCATransitionFromBottom;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [p_bg_img.layer addAnimation:animation forKey:@"animation"];
        animation.delegate = self;
        [self.layer addAnimation:animation forKey:@"animation"];
        
        p_bg_img.hidden = TRUE;
        self.hidden = TRUE;
        p_preview.hidden = TRUE;
        p_del_btn.hidden = TRUE;
        p_label_file_size.hidden = TRUE;
        p_label_file_size_val.hidden = TRUE;
        p_label_duration.hidden = TRUE;
        p_label_duration_val.hidden = TRUE;
    }
}

@end
